import pygame
import random
from contants import*

DIRECTION_DICT={
    pygame.K_UP:(0,-BLOCK_SIZE),#上
    pygame.K_RIGHT:(BLOCK_SIZE ,0),#右
    pygame.K_DOWN:(0, BLOCK_SIZE),#下
    pygame.K_LEFT:(-BLOCK_SIZE,0),#左             
}

HEAD_DICT ={
            pygame.K_DOWN : 0,
            pygame.K_UP: 180,
            pygame.K_LEFT : 270,
            pygame.K_RIGHT: 90,
            
        } 

class Snake:
    def __init__(self,x,y) -> None:
        self.direction = pygame.K_LEFT  #0上 1右 2下 3左
        self.is_cross_enable = True
        self.snake_body = [
            pygame.Rect(x * BLOCK_SIZE,y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE),
        ] 
        head_image = pygame.image.load("res/a513dc853f5f532157bec96d368f098.jpg")
        #缩放
        self.snake_head_image=pygame.transform.scale(head_image,(BLOCK_SIZE,BLOCK_SIZE))
        
        self.score = 0
        for _ in range(2):
            self.grow()
        
    def draw(self,screen):
        
        #绘制蛇身体
        for node in self.snake_body[1:]:
                pygame.draw.rect(screen,COLOR_WHITE,node,width=4,border_radius=3)
        
        #绘制蛇头
        head: pygame.Rect = self.snake_body[0]
        #对原始的头旋转 ：
        # Right->90
        # Down ->0
        # UP   ->180 
        # Left ->270  
        head_image = pygame.transform.rotate(self.snake_head_image,HEAD_DICT[self.direction])
        screen.blit(head_image,(head.x,head.y))
        
        
    def is_direction_enable(self,input_key):
        if input_key not in (pygame.K_RIGHT, pygame.K_LEFT,pygame.K_UP,pygame.K_DOWN):
            return False
        
        LR = (pygame.K_RIGHT, pygame.K_LEFT)
        if self.direction in LR and input_key in LR :
            return False
        UP = (pygame.K_UP,pygame.K_DOWN)
        if self.direction in UP and input_key in UP :
            return False
        return True
    
    
    def update_direction(self,input_key):
        #禁止水平或竖直方向直接转换
        #蛇不能原地掉头（不能左右）
        #蛇不能原地掉头（不能向上）
       

        
        self.direction = input_key
    
    
    def move(self):
        new_node = self.snake_body[0].copy()
        new_move = DIRECTION_DICT[self.direction]
        new_node.x += new_move[0]
        new_node.y += new_move[1]
        
        
        if self.is_cross_enable:
            
            #如果超出范围，瞬移
            if new_node.x >= SCREEN_WIDTH:
                new_node.x -= SCREEN_WIDTH
            elif new_node.x < 0:
                new_node.x += SCREEN_WIDTH

            if new_node.y >= SCREEN_HEIGHT:
                new_node.y -= SCREEN_HEIGHT
            elif new_node.y<0:
                new_node.y += SCREEN_HEIGHT
    
         
        #把新的蛇头放在最前面
        self.snake_body.insert(0,new_node)
        #删除蛇尾巴
        self.snake_body.pop()

    def grow(self):
        #复制蛇尾
        new_node = self.snake_body[-1].copy()
        self.snake_body.append(new_node)
        
        #得1分
        self.score += 1

class Food:
    def __init__(self,node) -> None:
        self.node = node
    
    def draw(self,screen):
        pygame.draw.rect(screen,COLOR_BLUE,self.node,border_radius=3)
        
    @staticmethod
    def gen_food_positison(snake:Snake):
        while True:
            
            x = random.randint(0,SCREEN_WIDTH // BLOCK_SIZE-1)
            
            y = random.randint(0,SCREEN_HEIGHT // BLOCK_SIZE-1)
            
            new_food_node = pygame.Rect(x * BLOCK_SIZE,y * BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE)
            
            if new_food_node not in snake.snake_body:
                #新的食物不在蛇身和蛇头
                return new_food_node

        